<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="${dao_package_name}.${table_name}Mapper">
    <resultMap id="BaseResultMap" type="${response_package_name}.${table_name}Response" >
        <id column="id" property="id" jdbcType="BIGINT" />
        <#list model_column as model>
        <#if (model.columnName != 'id')>
        <result column="${model.columnName}" property="${model.changeColumnName?uncap_first}" jdbcType="${model.columnType}" />
        </#if>
        </#list>
    </resultMap>
    <sql id="Base_Column_List" >
    <#list model_column as model><#if model_has_next>${model.columnName},<#else>${model.columnName}</#if></#list>
    </sql>

    <sql id="where_sql">
    <#list model_column as model>
        <#if (model.javaColumnType == "String")>
        <if test="${model.changeColumnName?uncap_first} != null">
            and ${model.columnName} LIKE  <![CDATA[CONCAT ('%',${r'#'}{${model.changeColumnName?uncap_first},jdbcType=${model.columnType}},'%')]]>
        </if>
        <#else>
        <if test="${model.changeColumnName?uncap_first} != null">
            and ${model.columnName} = ${r'#'}{${model.changeColumnName?uncap_first},jdbcType=${model.columnType}}
        </if>
        </#if>
    </#list>
    </sql>

    <delete id="delete${table_name}ById" parameterType="java.lang.Long">
        delete from ${ori_table}
        WHERE id = ${r'#{id,jdbcType=BIGINT}'}
    </delete>

    <delete id="delete${table_name}ByRequest" parameterType="${request_package_name}.${table_name}Request">
        delete from ${ori_table}
        WHERE id = ${r'#{id,jdbcType=BIGINT}'}
        <#if (content_shop_id)>
        AND shop_id = ${r'#{shopId,jdbcType=BIGINT}'}
        </#if>
        <#if (content_uid)>
        AND uid = ${r'#{uid,jdbcType=BIGINT}'}
        </#if>
    </delete>

    <insert id="insert${table_name}ByRequest" parameterType="${request_package_name}.${table_name}Request" >
        insert into ${ori_table}
        <trim prefix="(" suffix=")" suffixOverrides="," >
        <#list model_column as model>
            <if test="${model.changeColumnName?uncap_first} != null" >
            ${model.columnName},
            </if>
        </#list>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides="," >
        <#list model_column as model>
            <if test="${model.changeColumnName?uncap_first} != null" >
            ${r'#'}{${model.changeColumnName?uncap_first},jdbcType=${model.columnType}},
            </if>
        </#list>
        </trim>
    </insert>

    <update id="update${table_name}ByRequest" parameterType="${request_package_name}.${table_name}Request" >
        update ${ori_table}
        <set >
            <#list model_column as model>
            <if test="${model.changeColumnName?uncap_first} != null" >
            ${model.columnName} = ${r'#'}{${model.changeColumnName?uncap_first},jdbcType=${model.columnType}},
            </if>
            </#list>
        </set>
        WHERE id = ${r'#{id,jdbcType=BIGINT}'}
        <#if (content_shop_id)>
        AND shop_id = ${r'#{shopId,jdbcType=BIGINT}'}
        </#if>
        <#if (content_uid)>
        AND uid = ${r'#{uid,jdbcType=BIGINT}'}
        </#if>
    </update>

    <select id="select${table_name}ByRequest" keyProperty="id" useGeneratedKeys="true" resultMap="BaseResultMap" parameterType="${request_package_name}.${table_name}Request" >
        select
        <include refid="Base_Column_List" />
        from ${ori_table}
        WHERE id = ${r'#{id,jdbcType=BIGINT}'}
        <#if (content_shop_id)>
        AND shop_id = ${r'#{shopId,jdbcType=BIGINT}'}
        </#if>
         <#if (content_uid)>
        AND uid = ${r'#{uid,jdbcType=BIGINT}'}
        </#if>
        <#if (content_delete_flag)>
        AND delete_flag = 0
        </#if>
    </select>

    <select id="select${table_name}ById" resultMap="BaseResultMap" parameterType="java.lang.Long" >
        select
        <include refid="Base_Column_List" />
        from ${ori_table}
        WHERE id = ${r'#{id,jdbcType=BIGINT}'}
        <#if (content_delete_flag)>
        AND delete_flag = 0
        </#if>
    </select>

    <!-- 列表分页查询-->
    <select id="select${table_name}ForPage" resultMap="BaseResultMap" parameterType="${request_package_name}.${table_name}Request" >
        select
        <include refid="Base_Column_List" />
        from ${ori_table} where 1=1
        <include refid="where_sql" />
        <#if (content_delete_flag)>
        AND delete_flag = 0
        </#if>
        order by id Desc
    </select>
    <!-- 查询全部-->
    <select id="select${table_name}All" resultMap="BaseResultMap" parameterType="${request_package_name}.${table_name}Request" >
        select
        <include refid="Base_Column_List" />
        from ${ori_table} where 1=1
        <include refid="where_sql" />
        <#if (content_delete_flag)>
        AND delete_flag = 0
        </#if>
        order by id Desc
    </select>

    <!-- 根据主键集合查询集合-->
    <select id="select${table_name}ByIds" resultMap="BaseResultMap" parameterType="java.util.List" >
        select
        <include refid="Base_Column_List" />
        from ${ori_table} where id IN
        <foreach collection="list" item="item" open="(" separator="," close=")">
            ${r'#{item}'}
        </foreach>
    </select>
</mapper>